package com.jiadeyu.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.jiadeyu.dto.ArticlePageDTO;
import com.jiadeyu.mapper.ArticleMapper;
import com.jiadeyu.pojo.Article;
import com.jiadeyu.service.ArticleService;
import com.jiadeyu.utils.ThreadLocalUtil;
import jakarta.annotation.Resource;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.Map;

@Service
public class ArticleServiceImpl implements ArticleService {
    @Resource
    private ArticleMapper articleMapper;

  @Override
  public void add(Article article) {
    Map<String, Object> map = ThreadLocalUtil.get("Authorization");
    Integer userId = (Integer) map.get("id");
    article.setCreateUser(userId);
    article.setCreateTime(LocalDateTime.now());
    article.setUpdateTime(LocalDateTime.now());
    articleMapper.add(article);
    }

  @Override
  public IPage<Article> list(IPage<Article> page, Integer categoryId, String state) {
    LambdaQueryWrapper<Article> queryWrapper = new LambdaQueryWrapper<>();
    queryWrapper.eq(null != categoryId, Article::getCategoryId, categoryId);
    queryWrapper.eq(null != state, Article::getState, state);
    Map<String, Object> map = ThreadLocalUtil.get("Authorization");
    Integer userId = (Integer) map.get("id");
    queryWrapper.eq(Article::getCreateUser, userId);
    return articleMapper.selectPage(page, queryWrapper);
  }
}
